package micotime.com.amap_plugin.map.common;

import android.graphics.BitmapFactory;

import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.flutter.plugin.common.MethodCall;
import micotime.com.amap_plugin.map.model.UnifiedMarkerOptions;

@SuppressWarnings("unchecked")
public class MarkerOptionsUtils {
    public final static UnifiedMarkerOptions getMarkerOptions(MethodCall call) {
        /*
        MarkerOptions markerOptions = new MarkerOptions();
        if (call.argument("lat") != null && call.argument("lng") != null) {
            markerOptions.position(new LatLng(Float.parseFloat(call.argument("lat").toString()),
                    Float.parseFloat(call.argument("lng").toString())));
        }
        if (call.argument("title") != null) {
            markerOptions.title(call.argument("title").toString());
        }
        if (call.argument("draggable") != null) {
            markerOptions.draggable((boolean) call.argument("draggable"));
        }
        if (call.argument("flat") != null) {
            markerOptions.setFlat((boolean) call.argument("flat"));
        }
        if (call.argument("isGps") != null) {
            markerOptions.setGps((boolean) call.argument("isGps"));
        }
        if (call.argument("snippet") != null) {
            markerOptions.snippet(call.argument("snippet").toString());
        }
        if (call.argument("alpha") != null) {
            markerOptions.alpha(Float.parseFloat(call.argument("alpha").toString()));
        }
        if (call.argument("infoWindowEnable") != null) {
            markerOptions.infoWindowEnable((boolean) call.argument("infoWindowEnable"));
        }
        if (call.argument("isVisible") != null) {
            markerOptions.visible((boolean) call.argument("isVisible"));
        }
        if (call.argument("period") != null) {
            markerOptions.period((int) call.argument("period"));
        }
        if (call.argument("rotateAngle") != null) {
            markerOptions.rotateAngle(Float.parseFloat(call.argument("rotateAngle").toString()));
        }
        if (call.argument("zIndex") != null) {
            markerOptions.zIndex(Float.parseFloat(call.argument("zIndex").toString()));
        }
        if (call.argument("infoWindowOffset_offsetX") != null
                && call.argument("infoWindowOffset_offsetY") != null) {
            markerOptions.setInfoWindowOffset((int) call.argument("infoWindowOffset_offsetX"),
                    (int) call.argument("infoWindowOffset_offsetY"));
        }

        if ( call.argument("icon_data") != null) {
            byte[] data =  (byte[]) call.argument("icon_data");
            markerOptions.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeByteArray(data,0,data.length)));
        }
        Map<String,Object> animation = null;
        if(call.argument("markerAnimation") !=null){
            animation  = (HashMap<String, Object>)call.argument("markerAnimation");

        }
        UnifiedMarkerOptions unitfiedMarkerOptions = new UnifiedMarkerOptions(markerOptions, animation);
        return unitfiedMarkerOptions;
         */
        return getMarkerOptions((HashMap<String,Object>)call.arguments);
    }

    public final static UnifiedMarkerOptions getMarkerOptions(Map<String,Object> map) {
        MarkerOptions markerOptions = new MarkerOptions();
        if (map.get("lat") != null && map.get("lng") != null) {
            markerOptions.position(new LatLng(Float.parseFloat(map.get("lat").toString()),
                    Float.parseFloat(map.get("lng").toString())));
        }
        if (map.get("title") != null) {
            markerOptions.title(map.get("title").toString());
        }
        if (map.get("draggable") != null) {
            markerOptions.draggable((boolean) map.get("draggable"));
        }
        if (map.get("flat") != null) {
            markerOptions.setFlat((boolean)map.get("flat"));
        }
        if (map.get("isGps") != null) {
            markerOptions.setGps((boolean)map.get("isGps"));
        }
        if (map.get("snippet") != null) {
            markerOptions.snippet(map.get("snippet").toString());
        }
        if (map.get("alpha") != null) {
            markerOptions.alpha(Float.parseFloat(map.get("alpha").toString()));
        }
        if (map.get("infoWindowEnable") != null) {
            markerOptions.infoWindowEnable((boolean)map.get("infoWindowEnable"));
        }
        if (map.get("isVisible") != null) {
            markerOptions.visible((boolean)map.get("isVisible"));
        }
        if (map.get("period") != null) {
            markerOptions.period((int)map.get("period"));
        }
        if (map.get("rotateAngle") != null) {
            markerOptions.rotateAngle(Float.parseFloat(map.get("rotateAngle").toString()));
        }
        if (map.get("zIndex") != null) {
            markerOptions.zIndex(Float.parseFloat(map.get("zIndex").toString()));
        }
        if (map.get("infoWindowOffset_offsetX") != null
                && map.get("infoWindowOffset_offsetY") != null) {
            markerOptions.setInfoWindowOffset((int)map.get("infoWindowOffset_offsetX"),
                    (int)map.get("infoWindowOffset_offsetY"));
        }

        if (map.get("icon_data") != null) {
            byte[] data =  (byte[])map.get("icon_data");
            markerOptions.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeByteArray(data,0,data.length)));
        }
        Map<String,Object> animation = null;
        if(map.get("markerAnimation") !=null){
            animation  = (HashMap<String, Object>)map.get("markerAnimation");

        }
        UnifiedMarkerOptions unitfiedMarkerOptions = new UnifiedMarkerOptions(markerOptions, animation);
        return unitfiedMarkerOptions;
    }

    /**
     * 获取多个marker
     * @return
     */
    @SuppressWarnings("unchecked")
    public final static List<UnifiedMarkerOptions> getMarkerOptionsList(ArrayList<Map> list){
        List<UnifiedMarkerOptions> rev = new ArrayList<>();
        if(list ==null)return rev;
        for (Map map : list) {
            if(!(map instanceof HashMap))continue;
            HashMap<String,Object> hashMap = (HashMap<String,Object>)map;
            rev.add(getMarkerOptions(map));
        }
        return rev;
    }
}
